######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate the Excel Service
# -----------------------------------------------------------------
function New-ExcelService()
{
    $obj=new-object PSObject -Property @{
      Name = "Excel Service";
      Proxy = "Excel Service Proxy";
      Pool = New-ApplicationPool;
      Service = New-SupportingService;
      CachingOfUnusedFilesEnable = $true;
      CrossDomainAccessAllowed = $false;
      Default = $true;
      EncryptedUserConnectionRequired = "None";
      ExternalDataConnectionLifetime = 1800;
      FileAccessMethod = "UseImpersonation";
      LoadBalancingScheme = "Local";
      MemoryCacheThreshold = 90;
      PrivateBytesMax = -1;
      SessionsPerUserMax = 25;
      SiteCollectionAnonymousSessionsMax = 25;
      TerminateProcessOnAccessViolation = $true;
      UnattendedAccountApplicationId = "";
      UnusedObjectAgeMax = -1;
      ThrottleAccessViolationsPerSiteCollection = 0;
      WorkbookCache = "";
      WorkbookCacheSizeMax = 0;
    }
    $obj.Service.Name = "Excel Calculation Services"
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "Validating Excel Service"
        $this.Pool.Validate()
        $this.Service.Validate()
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "Proxy" $this.Proxy  
        Validate-NotEmpty "WorkbookCache" $this.WorkbookCache  
        Validate-MemberOf "EncryptedUserConnectionRequired" $this.EncryptedUserConnectionRequired "None","Connection"
        Validate-MemberOf "FileAccessMethod" $this.FileAccessMethod "UseImpersonation","UseFileAccessAccount"
        Validate-MemberOf "LoadBalancingScheme" $this.LoadBalancingScheme "WorkbookUrl","WorkbookUrl","Local"
        Validate-Range "MemoryCacheThreshold" $this.MemoryCacheThreshold 0 95
        Validate-Range "UnusedObjectAgeMax" $this.UnusedObjectAgeMax -1 34560
        Validate-Range "ThrottleAccessViolationsPerSiteCollection" $this.ThrottleAccessViolationsPerSiteCollection 0 2073600
    }
    # -----------------------------------------------------------------
    # Provisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        $sa = Get-SPServiceApplication  | Where-Object {$_.DisplayName -eq $this.Name}

        if($sa -eq $null)
        {
            Write-Operation "Creating $($this.Name)"
            $pool = $this.Pool.Provision()
            
            $sa = New-SPExcelServiceApplication -Name $this.Name `
                    -ApplicationPool $pool `
                    -CachingOfUnusedFilesEnable:$this.CachingOfUnusedFilesEnable `
                    -CrossDomainAccessAllowed:$this.CrossDomainAccessAllowed `
                    -Default:$this.Default `
                    -EncryptedUserConnectionRequired $this.EncryptedUserConnectionRequired `
                    -ExternalDataConnectionLifetime $this.ExternalDataConnectionLifetime `
                    -FileAccessMethod $this.FileAccessMethod `
                    -LoadBalancingScheme $this.LoadBalancingScheme `
                    -MemoryCacheThreshold $this.MemoryCacheThreshold `
                    -PrivateBytesMax $this.PrivateBytesMax `
                    -SessionsPerUserMax $this.SessionsPerUserMax `
                    -SiteCollectionAnonymousSessionsMax $this.SiteCollectionAnonymousSessionsMax `
                    -TerminateProcessOnAccessViolation:$this.TerminateProcessOnAccessViolation `
                    -UnattendedAccountApplicationId $this.UnattendedAccountApplicationId `
                    -UnusedObjectAgeMax $this.UnusedObjectAgeMax
        }
        else
        {
            Write-OperationDone "$($this.Name) has already been provisioned"  
        } 
        
        Set-SPExcelServiceApplication -Identity $sa `
            -ThrottleAccessViolationsPerSiteCollection $this.ThrottleAccessViolationsPerSiteCollection `
            -WorkbookCache $this.WorkbookCache `
            -WorkbookCacheSizeMax $this.WorkbookCacheSizeMax 
        
        # proxy automatically created
        $this.Service.Provision()
        return $sa
    }
    # -----------------------------------------------------------------
    # Unprovisions the Service
    # -----------------------------------------------------------------
    Add-Method $obj "Unprovision" `
    {
        Remove-ServiceApplicationProxy $this.Proxy
        Remove-ServiceApplication $this.Name
        $this.Service.Unprovision()
    }
    return $obj
}
